package com.platform.dto;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.platform.comm.constants.GlobalConstant;
import com.platform.comm.constraints.CImport;
import com.platform.comm.validation.groups.Batch;
import com.platform.comm.validation.groups.Insert;
import com.platform.comm.validation.groups.Modify;
import com.platform.enums.CommFlagType;
import com.platform.validator.IdCode;
import com.platform.validator.Mobile;
import com.platform.comm.dto.BaseQo;
import com.platform.comm.enums.YnEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * Description: 用户DTO
 *
 * @author libin Created on 2019/7/16
 **/
@EqualsAndHashCode(callSuper = true)
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class UserQo extends BaseQo {

    private static final long serialVersionUID = 2945199532259185940L;
    @NotEmpty(message = "用户名不能为空", groups = {Insert.class, Batch.class, Modify.class})
    @Size(min = 4, max = 128, message = "用户名必须大于3个字符且小于128个字符", groups = {Insert.class, Batch.class, Modify.class})
    @CImport(name = "用户名")
    @Pattern(regexp = "^[a-zA-Z0-9][a-zA-Z0-9_]*$", message = "用户名只能包含字母、数字、下划线组合", groups = {Insert.class, Batch.class, Modify.class})
    private String username;

    @NotEmpty(message = "真实名称不能为空", groups = {Insert.class, Batch.class})
    @Size(min = 1, max = 50, message = "真实名称长度不能超过50个字符", groups = {Insert.class, Batch.class})
    @CImport(name = "真实名称")
    private String realName;

    @Size(max = 50, message = "角色名称长度不能超过128个字符", groups = {Batch.class})
    @CImport(name = "角色名称")
    private String roleName;

    @Size(min = 6, max = 256, message = "密码至少6位字符")
    private String password;

    @JsonIgnore
    @Min(value = -1)
    private Integer pwdLimitDays;

    @JsonFormat(pattern = GlobalConstant.DATE_TIME_PATTERN, shape = JsonFormat.Shape.STRING)
    @DateTimeFormat(pattern = GlobalConstant.DATE_TIME_PATTERN)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime pwdModifyDate;

    @Size(max = 36)
    @NotEmpty(message = "所属部门不能为空", groups = {Insert.class, Batch.class})
    @CImport(name = "所属部门")
    private String departId;

    private String departName;

    //部门类型
    private String departKind;

    @Pattern(regexp = "[01YN]", message = "信息状态不合法")
    private YnEnum delFlag;

    @Size(max = 3)
    @Pattern(regexp = "[IYDLP]", message = "用户状态值不匹配")
    private CommFlagType status;

    @Email
    @Size(max = 64, message = "邮箱长度不能超过64个字符", groups = {Insert.class, Batch.class})
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String email;

    @CImport(name = "手机号码")
    @Mobile(message = "手机号格式不合法", groups = {Insert.class, Batch.class})
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String mobile;

    @Size(max = 128)
    private String tel;

    @Min(value = 0)
    private Integer sortNum;

    @Size(max = 1024, message = "备注长度不能超过1024个字符", groups = {Insert.class, Batch.class})
    private String remark;

    @Size(max = 128)
    private String userCode;

    @Size(max = 256)
    private String infoSource;

    @Size(max = 64)
    @IdCode(message = "身份证格式不正确", groups = {Insert.class, Batch.class, Modify.class})
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String idCode;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @Size(max = 36)
    private String domain;

    @Size(max = 36)
    private String importId;

    @Size(max = 20)
    private String importCheckFlag;

    @Size(max = 128)
    private String checkErrMsg;

    @JsonIgnoreProperties({"createDate", "updateDate", "parentName", "parentId", "delFlag", "route", "desc"})
    private List<RoleQo> roles;

    @JsonIgnoreProperties({"createDate", "updateDate", "parentName", "parentId", "delFlag", "route", "syncStatus","remark","importId","importCheckFlag","checkErrMsg","children","userList","status","sortNum","infoSource"})
    private List<DepartQo> posts;


    @Size(max = 3)
    private String syncStatus;


    private Boolean isAdmin;

    private String userType;

    private String avatarFileId;

    @Override
    public void init() {
        super.init();
        this.setDelFlag(YnEnum.N);
        this.setStatus(CommFlagType.Enabled);
        if (this.getPwdLimitDays() == null) {
            this.setPwdLimitDays(-1);
        }
        this.setPwdModifyDate(LocalDateTime.now());
        if (this.getSortNum() == null) {
            this.setSortNum(0);
        }
        this.setInfoSource(Objects.toString(this.getInfoSource(),"local"));
        this.setUserCode(this.getUserCode());
        this.setPassword("123456");
    }
}
